package com.pkk.components.handler;

import com.pkk.components.exceptions.BusinessException;
import com.pkk.components.rpc.common.code.CommonCode;
import com.pkk.components.moudle.oauth2.AuthServiceException;
import com.pkk.components.moudle.oauth2.AuthUserException;
import com.pkk.components.moudle.oauth2.Oauth2Accessexception;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 业务异常处理类
 * @author: peikunkun
 * @create: 2020-03-09 18:01
 **/
@RestControllerAdvice
public class BusinessExceptionHandle {


  /**
   * @Description: 参数校验的异常返回
   * @Param: exception
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Object>
   * @Author: peikunkun
   * @Date: 2020/3/10 0010 上午 11:19
   */
  @ExceptionHandler(value = MethodArgumentNotValidException.class)
  public CommonResponse<Object> MethodArgumentNotValidHandler(MethodArgumentNotValidException exception) {
    //按需重新封装需要返回的错误信息
    String errorMsg = null;
    //解析原错误信息，封装后返回，此处返回非法的字段名称，错误信息
    for (FieldError error : exception.getBindingResult().getFieldErrors()) {
      //paramValidationResults.put(error.getField(), error.getDefaultMessage());
      errorMsg = error.getDefaultMessage();
      if (StringUtils.isNotBlank(errorMsg)) {
        break;
      }
    }
    return ResponseUtil.error(CommonCode.PARAM_ERROR, errorMsg);
  }


  /**
   * @Description: 自定义oauth2授权异常类
   * @Param: e
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Object>
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 上午 11:26
   */
  @ExceptionHandler(AuthServiceException.class)
  public CommonResponse<Object> authServiceException(AuthServiceException e) {
    return ResponseUtil.error(e.getCodeEnum());
  }

  /**
   * @Description: 自定义oauth2授权异常类
   * @Param: e
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Object>
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 上午 11:26
   */
  @ExceptionHandler(AuthUserException.class)
  public CommonResponse<Object> authUserException(AuthUserException e) {
    return ResponseUtil.error(e.getCodeEnum());
  }


  /**
   * @Description: 自定义oauth2授权异常类
   * @Param: e
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Object>
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 上午 11:26
   */
  @ExceptionHandler(Oauth2Accessexception.class)
  public CommonResponse<Object> oauth2Accessexception(Oauth2Accessexception e) {
    return ResponseUtil.error(e.getCodeEnum());
  }


  /**
   * @Description: 业务异常处理类
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Object>
   * @Author: peikunkun
   * @Date: 2020/3/9 0009 下午 6:04
   */
  @ExceptionHandler(BusinessException.class)
  public CommonResponse<Object> businessException(BusinessException e) {
    return ResponseUtil.error(e.getCodeEnum());
  }

  /**
   * @Description: 服务处理异常runtime
   * @Param: e
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Object>
   * @Author: peikunkun
   * @Date: 2020/3/23 0023 下午 5:49
   */
  public CommonResponse<Object> runtimeException(RuntimeException e) {
    return ResponseUtil.error(e.getMessage());
  }

  /**
   * @Description: 异常处理类
   * @Param: e
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Object>
   * @Author: peikunkun
   * @Date: 2020/3/10 0010 上午 9:48
   */
  @ExceptionHandler(Exception.class)
  public CommonResponse<Object> exception(Exception e) {
    return ResponseUtil.error(e.getMessage());
  }


}
